package com.admin.service.login_verify_service;

import com.admin.dao.adminlogindao.AdminDaoImpl;
import com.pojo.AdminBean;
import com.utils.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class LoginVerifyServiceImpl implements LoginVerifyService{
    private AdminDaoImpl adminDao = new AdminDaoImpl();

    /**
     * 登录验证
     * @param ab
     * @return
     */
    @Override
    public Boolean adminCheck(AdminBean ab) {
        boolean bool = false;

        Connection conn = null;

        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);//关闭自动提交
            String sql = "select * from admin where adminname=? and adminpwd=?";
            bool = adminDao.adminLoginCheck(sql,conn,ab);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return bool;
    }

    public AdminBean getAdminAllInfo(String adminName){
        AdminBean adminBeanList =null;
        Connection conn = null;

        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);//关闭自动提交
            String sql = "select * from admin where adminname=? ";
            adminBeanList = adminDao.getAdminAllInfoDao(sql,conn,adminName);

            conn.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return adminBeanList;
    }

    /**
     * 管理员注册
     * @param ab
     * @return
     */
    @Override
    public Boolean adminRegister(AdminBean ab) {
        boolean bool = false;

        Connection conn = null;

        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);//关闭自动提交
            String sql = "insert into admin(adminname,adminpwd,adminphone,adminlevel) values(?,?,?,?)";
            bool = adminDao.adminRegister(sql,conn,ab);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return bool;
    }

    /**
     * 注册验证
     * @param ab
     * @return
     */
    @Override
    public Boolean regAdminCheck(AdminBean ab) {
        boolean bool = false;

        Connection conn = null;

        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);//关闭自动提交
            String sql = "select * from admin where adminname=? and adminphone=?";
            bool = adminDao.adminRegisterCheck(sql,conn,ab);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return bool;
    }

}
